define([
    "helpers/network",
    "helpers/util",
], function (Network, Util) {
    var id = "";
    var tableId = id + "table";
    var logTableId = id + "logTable";
    var timer;

    /**
     * 刷新数据
     * @type {number}
     */
    var countdown = 0;//60秒

    var verifierSms = function (data) {
        timer = setInterval(function () {
            if (countdown == 30) {
                onTableReady();
                clearInterval(timer);
                countdown = 0;
            } else {
                countdown++;
            }
        }, 1000)
    }

    var onTableReady = function () {
        verifierSms(1)
        //彩店线上情况
        Network.ajax("/ConductorView.sumConductor.query", {params: {}}, function (ret) {
            var sumConductorData = ret.data
            var onlineNumber = sumConductorData.onlineNumber
            var workingNumber = sumConductorData.workingNumber
            var offlineNumber = sumConductorData.offlineNumber
            $$("onlineNumber").define("label", "<span style='color:red'>" + onlineNumber + "</span> 家彩店接单中")
            $$("onlineNumber").refresh()

            $$("workingNumber").define("label", "<span style='color:green'>" + workingNumber + "</span> 家彩店出票中")
            $$("workingNumber").refresh()

            $$("offlineNumber").define("label", "<span style='color:black'>" + offlineNumber + "</span> 家彩店已离线")
            $$("offlineNumber").refresh()

            $$("todaySurplusSaleAmount").define("label", "今日出票剩余额度: <span style='color:red'>" + (Number(sumConductorData.todaySurplusSaleAmount) / 10000).toCurrency() + "</span>万")
            $$("todaySurplusSaleAmount").refresh()

            //统计画布
            $$("sumConductorId").clearAll();
            $$("sumConductorId").add({shops: onlineNumber, status: "接单中", color: "#04d4e0"})
            $$("sumConductorId").add({shops: workingNumber, status: "出票中", color: "#f79a00"})
            $$("sumConductorId").add({shops: offlineNumber, status: "已离线", color: "#8bd16e"})

        })

        //配额使用情况
        Network.ajax("/UserView.sumQuotaUpperLimit.query", {params: {}}, function (ret) {
            var sumQuotaUpperLimitData = ret.data
            var MaximumPlatformQuotaUpperLimit = (Number(sumQuotaUpperLimitData.MaximumPlatformQuotaUpperLimit)) / 10000
            var sumQuotaUpperLimit = (Number(sumQuotaUpperLimitData.sumQuotaUpperLimit)) / 10000
            var surplusQuotaUpperLimit = (Number(sumQuotaUpperLimitData.surplusQuotaUpperLimit)) / 10000
            $$("MaximumPlatformQuotaUpperLimit").define("label", "总共配额<span style='color:red'>" + MaximumPlatformQuotaUpperLimit.toCurrency() + "</span>万")
            $$("MaximumPlatformQuotaUpperLimit").refresh()

            $$("sumQuotaUpperLimit").define("label", "配额分配<span style='color:red'>" + sumQuotaUpperLimit.toCurrency() + "</span>万")
            $$("sumQuotaUpperLimit").refresh()

            $$("surplusQuotaUpperLimit").define("label", "剩余配额<span style='color:green'>" + surplusQuotaUpperLimit.toCurrency() + "</span>万")
            $$("surplusQuotaUpperLimit").refresh()

            //统计画布
            $$("sumQuotaUpperLimitId").clearAll();
            $$("sumQuotaUpperLimitId").add({value: sumQuotaUpperLimit.toCurrency(), status: "已使用", color: "#f79a00"})
            $$("sumQuotaUpperLimitId").add({value: surplusQuotaUpperLimit.toCurrency(), status: "剩余", color: "#8bd16e"})
        })

        //操作日期
        var obj = {
            params: {},
            size: 20,
            sort: ["creationTime,desc"]
        }
        Network.ajaxLog("/queryAll.do", obj, function (ret) {
            var content = ret.data.content;
            Network.ajax("/AdminView.queryAll.query", {params: {}, all: true}, function (ret) {
                adminContent = ret.data.content;
                for (var i = 0; i < content.length; i++) {
                    var operatorId = content[i].operatorId
                    for (var a = 0; a < adminContent.length; a++) {
                        var id = adminContent[a].id
                        if (Number(operatorId) == id) {
                            content[i]["adminData"] = adminContent[a]
                        }
                    }
                }
                $$(logTableId).clearAll();
                $$(logTableId).parse(content);
            })
        })
    }

    var genSummaryUI = function () {
        return {
            cols: [
                {
                    view: "fieldset", label: "系统概况", width: 900,
                    body: {
                        height: 250,
                        cols: [
                            {
                                gravity: 2,
                                rows: [
                                    {
                                        view: "form", borderless: true, id: "sumConductorView",
                                        elements: [
                                            {
                                                id: "onlineNumber",
                                                view: "label",
                                                label: "<span style='color:red'></span> 家彩店接单中"
                                            },
                                            {
                                                id: "workingNumber",
                                                view: "label",
                                                label: "<span style='color:green'></span> 家彩店出票中"
                                            },
                                            {
                                                id: "offlineNumber",
                                                view: "label",
                                                label: "<span style='color:black'></span> 家彩店已离线"
                                            },
                                            {
                                                id: "todaySurplusSaleAmount",
                                                view: "label",
                                                label: "今日出票剩余额度: <span style='color:red'></span>万"
                                            }
                                        ]
                                    }
                                ]
                            },
                            {
                                id: "sumConductorId", gravity: 2, view: "chart",
                                borderless: true, type: "pie", value: "#shops#",
                                color: "#color#", label: "#status#", pieInnerText: "#shops#",
                                shadow: 0, radius: 65,
                                data: []
                            },
                            {
                                gravity: 1,
                                rows: [
                                    {
                                        id: "MaximumPlatformQuotaUpperLimit",
                                        view: "label",
                                        label: "总共配额<span style='color:red'>0.00</span>万"
                                    },
                                    {
                                        id: "sumQuotaUpperLimit",
                                        view: "label",
                                        label: "配额使用<span style='color:red'>0.00</span>万"
                                    },
                                    {
                                        id: "surplusQuotaUpperLimit",
                                        view: "label",
                                        label: "剩余配额<span style='color:green'>0.00</span>万"
                                    }
                                ]
                            },
                            {
                                id: "sumQuotaUpperLimitId",
                                gravity: 2,
                                view: "chart",
                                borderless: true,
                                type: "pie",
                                value: "#value#",
                                color: "#color#",
                                label: "#status#",
                                pieInnerText: function (obj) {
                                    return "<span style='color:black'>" + obj.value + "</span>";
                                },
                                shadow: 0,
                                radius: 65,
                                data: [
                                    // {value: "20.9", status: "已使用", color: "#f79a00"},
                                    // {value: "18.8", status: "剩余", color: "#8bd16e"}
                                ]
                            },
                        ]
                    }
                },
                {
                    view: "fieldset", label: "操作日志", gravity: 2, height: 250,
                    body: {
                        id: logTableId, view: "datatable", resizeColumn: true, select: "row",
                        // borderless:true,header:false,
                        data: [], complexData: true, ready: onTableReady,
                        on: {
                            onDestruct:function () {
                                if(timer) clearInterval(timer);
                            },
                            onItemClick: function (id) {
                            }
                        },
                        columns: [
                            {
                                id: "operatorId", header: "操作员", fillspace: true,
                                template: function (obj) {
                                    var adminData = obj.adminData;
                                    if (adminData) {
                                        return adminData.staff
                                    } else {
                                        return obj.operatorId
                                    }
                                }
                            },
                            {
                                id: "title", header: "操作内容", fillspace: true,
                                template: function (obj) {
                                    // return ApiMap.modelName(obj.operation)
                                }
                            },
                            {
                                id: "creationTime", header: "操作时间", fillspace: true
                            },
                        ]
                    }
                }
            ]

        }
    };

    var genOperationLogUI = function () {
        return {
            view: "fieldset", label: "收益情况",
            body: {
                id: tableId, view: "datatable", resizeColumn: true, select: "row", data: [],
                complexData: true,
                // ready: onTableReady,
                on: {
                    onItemClick: function (id) {

                    }
                },
                columns: [
                    {
                        id: "id", header: "ID"
                    },
                    {
                        id: "title", header: "操作员", fillspace: true,
                        template: "#operator#"
                    },
                    {
                        id: "operation", header: "操作描述", fillspace: true,
                        template: function (obj) {

                        }
                    },
                    {
                        id: "remark", header: "时间", fillspace: true,
                        template: function (obj) {
                            if (!obj.operateTime) return "";
                            return Util.getFormattedDateTime("yyyy-MM-dd hh:mm:ss", new Date(obj.operateTime))
                        }
                    }
                ]
            }
        }

    };

    var layout = {
        // type: "clean",
        padding: 20,
        rows: [{}
            // dashline,
            // genSummaryUI(),
            // genOperationLogUI()
        ]
    };

    return {$ui: layout};

});